package org.example.examfx02.controller;

import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import org.example.examfx02.entity.*;
import org.example.examfx02.tool.SystemData;

import java.util.List;

class MaxNumCell extends TableCell<Course, Integer> {
    @Override
    protected void updateItem(Integer item, boolean empty) {
        super.updateItem(item, empty);
        // 处理空单元格或无数据的情况
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            // 根据课程类型设置不同的显示内容
            Course course = getTableRow().getItem();
            if (course instanceof CourseRequire) {
                setText("——");
            } else if (course instanceof CourseChoose) {
                setText(((CourseChoose) course).getMaxNum().toString());
            }
        }
        // 设置单元格内容居中对齐
        setAlignment(Pos.CENTER);
    }
}
class CreditCell extends TableCell<Course, Integer> {
    @Override
    protected void updateItem(Integer item, boolean empty) {
        super.updateItem(item, empty);
        // 处理空单元格或无数据的情况
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            // 根据课程类型设置不同的显示内容
            Course course = getTableRow().getItem();
            if (course instanceof CourseRequire) {
                setText(((CourseRequire) course).getCredit().toString());
            } else if (course instanceof CourseChoose) {
                setText("——");
            }
        }
        // 设置单元格内容居中对齐
        setAlignment(Pos.CENTER);
    }
}
class TypeCell extends TableCell<Course, String> {
    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            Course course = getTableRow().getItem();
            if (course instanceof CourseRequire) {
                setText("必修");
            } else if (course instanceof CourseChoose) {
                setText("选修");
            }
        }
        setAlignment(Pos.CENTER);
    }
}
class UserChangedCell extends TableCell<User, String> {
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        // 处理空单元格或无数据的情况
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            // 根据用户的修改状态设置显示文本
            User user = getTableRow().getItem();
            if (user.getChanged()) {
                setText("已修改");
            } else {
                setText("未修改");
            }
        }
        // 设置单元格内容居中对齐
        setAlignment(Pos.CENTER);
    }
}
class TeacherNameCell extends TableCell<Course, String> {
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        // 处理空单元格或无数据的情况
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            // 获取课程对象并查找对应的教师姓名
            Course course = getTableRow().getItem();
            Teacher teacher = SystemData.getInstance().getUserGroup().getTeacherByID(course.getTeacherId());
            setText(teacher.getUsername());
        }
        // 设置单元格内容居中对齐
        setAlignment(Pos.CENTER);
    }
}
class HasChooseCell extends TableCell<Course, String> {
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        // 处理空单元格或无数据的情况
        if (empty || getTableRow() == null || getTableRow().getItem() == null) {
            setText(null);
        } else {
            // 获取课程对象并查找对应的教师姓名
            Course course = getTableRow().getItem();
            List<Course> list = ChooseCourseViewController.hasChooseList;
            if(list.contains(course)){
                setText("已选");
            }else{
                setText("未选");
            }
        }
        // 设置单元格内容居中对齐
        setAlignment(Pos.CENTER);
    }
}
